/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui.applicationframe.framesplitter;

import ftpcombiner.ApplicationController;
import ftpcombiner.filesystem.FileSystem;
import ftpcombiner.gui.GUILoader;
import ftpcombiner.gui.applicationframe.framesplitter.filetree.FileTreeModel;
import ftpcombiner.gui.applicationframe.framesplitter.filetree.FileTreeRenderer;
import ftpcombiner.server.virtualfiles.VirtualFolder;
import ftpcombiner.server.virtualfiles.VirtualItem;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;

/**
 *
 * @author Ben
 */
public class FileTree extends JTree implements GUILoader {

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    public FileTree() {
        init();
    }

    @Override
    public void initializeComponents() {
    }

    @Override
    public void addComponents() {
        //((DefaultTreeModel)getModel()).insertNodeInto(new DefaultMutableTreeNode("Hi"), (DefaultMutableTreeNode)((DefaultTreeModel)getModel()).getRoot(), 0);
        //System.out.println("node");
    }

    @Override
    public void addComponentListeners() {
        addTreeWillExpandListener(new TreeWillExpandListener() {
            @Override
            public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
                if (((VirtualFolder) event.getPath().getLastPathComponent()).getChildren() == null) {
                    System.out.println("willexpand");
                    FileSystem.getInstance().changeDirectory(((VirtualItem) event.getPath().getLastPathComponent()).getPath(), false, event.getPath());
                }
            }

            @Override
            public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
            }
        });
    }

    @Override
    public void otherInit() {
        //setPreferredSize(null);
        //setBackground(Color.RED);
        //System.out.println("filetree");
        setCellRenderer(new FileTreeRenderer());
        setModel(new FileTreeModel());
        //setVisible(true);
        ApplicationController.getSplashScreen().incrementProgress();
    }

    @Override
    public void setLayouts() {
    }

    public void update(TreePath path) {
        ((FileTreeModel) getModel()).fireTreeNodeLoaded(path);
//            fireTreeWillExpand(path);
    }
}
